<?php

class ContractController extends Controller
{
    public function filters()
    {
        return array(
            'accessControl',
            'postOnly + delete',
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('delete', 'index', 'view', 'create', 'update', 'datebb'),
                'users' => array('@'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public function actionDatebb($id)
    {
        $model = Contract::model()->findByPk($id);
        if (isset($_POST['value'])) {
            $model->datebb = date('d/m/Y', ($_POST['value']) / 1000);
            if ($model->save()) {
                echo json_encode(array('code' => '1'));
            }
        }

    }

    public function actionView($id)
    {
        $this->renderPartial('view', array(
            'contract' => $this->loadModel($id),
        ), false, true);
    }

    public function actionCreate()
    {
        $contract = new Contract();
        $product = new Product();
        if (isset($_POST['more'])) {
            $product = new Product();
            echo $this->renderPartial('_create', array(
                'product' => $product,
                'num' => $_POST['more']
            ), true);
            die();
        }
        $this->performAjaxValidation($contract);
        if (isset($_POST['Contract'])) {
            $contract->attributes = $_POST['Contract'];
            $contract->datelog = date("d-m-Y", time());
            $contract->userlog = Yii::app()->user->username;
            if ($contract->save()) {
                foreach ($_POST['Product'] as $product) {
                    echo '<pre>';
                    $p = new Product();
                    $this->performAjaxValidation($p);
                    $p->attributes = $product;
                    $p->contract_id = $contract->getPrimaryKey();
                    $p->save();
                }
                $this->redirect(array('index', 'id' => $contract->contract_id));
            }
        }
        $this->render('create', array(
            'contract' => $contract,
            'product' => $product,
        ));
    }

    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        $products = Product::model()->findAllByAttributes(array('contract_id' => $model->contract_id));
        if (isset($_POST['Contract'])) {
            $model->attributes = $_POST['Contract'];
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->contract_id));
        }

        $this->render('update', array(
            'contract' => $model,
            'products' => $products
        ));
    }

    public function actionDelete($id)
    {
        $this->loadModel($id)->delete();
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }

    public function actionIndex()
    {
        $model = new Contract('search');
        $model->unsetAttributes();
        if (isset($_GET['Contract'])) {
            $model->attributes = $_GET['Contract'];
        }
        $this->render('index', array(
            'model' => $model,
        ));
    }

    public function loadModel($id)
    {
        $model = Contract::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'contract-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
